package com.itheima.zuoye.zuoye3;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        String info = "10001,张无忌,男,2023-07-22 11:11:12," + "东湖-黄鹤楼" +
                "#10002," + "赵敏,女,2023-07-22 09:11:21," + "黄鹤楼-归元禅寺" +
                "#10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖" +
                "#10004，小昭,女,2023-07-22 08:11:21,东湖" +
                "#10005，灭绝,女,2023-07-22 17:11:21,归元禅寺";
        //定义一个集合
        ArrayList<Students> students = new ArrayList<>();
        //按照格式进行拆分
        String[] split = info.split("#");
        //定义日期格式化器
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //定义一个Map集合,
        // Map<Students, Integer> map = new HashMap<>();
        //定义一个景点统计器Map
        Map<String, Integer> map = new HashMap<>();

        //循环拆分数组
        for (String s : split) {
            s = s.replace("，", ",");
            String[] split1 = s.split(",");
            Integer id = Integer.parseInt(split1[0]);
            String name = split1[1];
            String sex = split1[2];
            LocalDateTime dateTime = LocalDateTime.parse(split1[3], dtf);
            String spot = split1[4];
            students.add(new Students(id, name, sex, dateTime.toString(), spot));
            students.forEach(System.out::println);
            System.out.println("添加成功！");
            System.out.println("===========");

            String[] split2 = spot.split("-");
            for (String sp : split2) {
                map.put(sp, map.getOrDefault(sp, 0) + 1);
            }
            System.out.println("===========");
            map.forEach((k, v) -> System.out.println(k + "访问次数=" + v));
            System.out.println("===========");
            Optional<Map.Entry<String, Integer>> max1 = map.entrySet().stream()
                    .max((entry1, entry2) -> entry1.getValue().compareTo(entry2.getValue()));
            System.out.println("访问人数最多的景点是：" + max1.get().getKey());
            System.out.println("=======");
            List<Students> sss = students.stream()
                    .filter(students1 -> !students1.getSpot().contains(max1.get().getKey()))
                    .collect(Collectors.toList());
            sss.forEach(students1 -> System.out.println(students1));


        }

    }
}
